from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

# 初始化浏览器驱动
driver = webdriver.Chrome(ChromeDriverManager().install())
time.sleep(2)
# 打开百度首页
driver.get("https://www.baidu.com")
time.sleep(2)

# 浏览器最大化
driver.maximize_window()
# # 获取浏览器尺寸
# print(driver.get_window_size())
# # 设置浏览器尺寸
# driver.set_window_size(800, 500)
# print(driver.get_window_size())

# # 获取浏览器位置
# print(driver.get_window_position())

# # 刷新页面
# driver.refresh()

# # 输入坚果
# input_element = driver.find_element(By.ID, "kw")   # 会拿到一个元素对象
# input_element.send_keys("坚果")
# time.sleep(2)
# # 点击-百度一下
# baidu_button = driver.find_element(By.ID, "su")
# baidu_button.click()
# time.sleep(2)
# # 回退
# driver.back()
# time.sleep(2)
# # 前进
# time.sleep(2)
# driver.forward()

# 获取当前页面url
print(driver.current_url)
# 获取当前页面title
print(driver.title)
# 当前页面截图
driver.get_screenshot_as_file(r'.\baidu.png')
# 获取页面源码
print(driver.page_source)
# 关闭浏览器
time.sleep(2)
driver.quit()
# 关闭当前标签页面
# driver.close()  # 如果是唯一窗口标签，会关闭浏览器